package com.bw.service.impl;

import com.bw.domain.Car;
import com.bw.domain.MonthlyCard;
import com.bw.domain.PlateType;
import com.bw.mapper.CarMapper;
import com.bw.service.CarService;
import com.bw.utils.MyEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author bobo棒
 * @date 2025年02月25日 11:31
 */

@Service
public class CarServiceImpl implements CarService {
    @Autowired
    CarMapper carMapper;

    @Override
    public List<Car> list(Car car) {
        return carMapper.list(car);
    }

    @Override
    public List<PlateType> listPlateType() {
        return carMapper.listPlateType();
    }

    //事务注解
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean add(Car car, MonthlyCard monthlyCard) throws MyEx {
        try {
            //添加到汽车表
            carMapper.add(car);
            //条件到月卡表
            //车辆和月卡进行绑定
            monthlyCard.setCarId(car.getId());
            //如果卡号不存在,则新增卡号并绑定车辆
            if(monthlyCard.getId()==null) {
                carMapper.addMonthlyCard(monthlyCard);
            }else{
                //如果卡号已经存在,则更新绑定车辆
                carMapper.updateMonthlyCard(monthlyCard);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            throw new MyEx("添加车辆失败");
        }

    }

    @Override
    public int checkPlateNumber(String plateNumber) {
        return carMapper.checkPlateNumber(plateNumber);
    }

    @Override
    public MonthlyCard getMontyCard(String cardNumber) {
        return carMapper.getMontyCard(cardNumber);
    }

    @Override
    public int batchDel(String ids) {
        return carMapper.batchDel(ids);
    }

    @Override
    public List<MonthlyCard> listCar() {
        return carMapper.listCar();
    }

    @Override
    public void updateStatus(MonthlyCard monthlyCard) {
        carMapper.updateStatus(monthlyCard);
    }

}
